﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express.Order.Order
{
    public partial class CustomerExportView : FormBase
    {
        #region Private Variables

        new CustomerExportPresenter _presenter;

        #endregion Private Variables

        public CustomerExportView()
        {
            InitializeComponent();

            cmbCustomerID.DisplayColumns.Add("PartnerCode", "Mã KH");
            cmbCustomerID.DisplayColumns.Add("PartnerName", "Tên KH");

            this.TopLevel = true;
            base._presenter = _presenter = new CustomerExportPresenter(this);
        }

        private void CustomerExportView_Load(object sender, EventArgs e)
        {
            LoadCarrier();
            LoadCustomer();
        }

        private void LoadCarrier()
        {
            try
            {
                Global.LoadComboBox(cmbCarrierID, base._presenter.LoadForCMB<CarrierENT>(null));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadCustomer()
        {
            try
            {
                SearchCriteria search = new SearchCriteria();
                search.Add("IsCustomer", 1);
                Global.LoadComboBox(cmbCustomerID, base._presenter.LoadForCMB<CustomerENT>(search));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Cursor = Cursors.WaitCursor;

                    string fileName = sfd.FileName;
                    string template = Path.Combine(Application.StartupPath, "Template\\Customer Export Template.xlt");
                    DataRowView carrierRow = cmbCarrierID.SelectedItem as DataRowView;
                    DataRowView customerRow = cmbCustomerID.SelectedItem as DataRowView;

                    if (carrierRow == null) throw new BusinessLogicException("Bạn phải chọn dịch vụ");
                    if (customerRow == null) throw new BusinessLogicException("Bạn phải chọn khách hàng");

                    DataTable dt = _presenter.LoadOrder(dtpFrom.Value, dtpTo.Value, Utility.IntParse(carrierRow["CarrierID"]), Utility.IntParse(customerRow["PartnerID"]));
                    if (dt == null || dt.Rows.Count == 0) throw new BusinessLogicException("Không có dữ liệu");

                    EnumerableRowCollection<DataRow> rows = dt.AsEnumerable();
                    int rateCount;
                    if ((rateCount = rows.GroupBy(r => r.Field<decimal?>("RATE")).Count()) > 1)
                    {
                        DialogResult result = MessageBox.Show("Dữ liệu về tỉ giá không đồng bộ trong khoảng thời gian đã chọn. Bạn có muốn tiếp tục export dữ liệu", String.Empty, MessageBoxButtons.YesNo);
                        if (result != DialogResult.Yes)
                            return;
                    }

                    _presenter.Export(fileName, carrierRow, customerRow, dt, rateCount, template);
                    DialogResult = DialogResult.Yes;
                }
            }
            catch (BusinessLogicException ex)
            {
                base.SetMessage(ex.Message, false);
            }
        }

        public DateTime From
        {
            set { dtpFrom.Value = value; }
        }

        public DateTime To
        {
            set { dtpTo.Value = value; }
        }
    }
}
